<!--
 * @Description: 创建班级数据统计
 * @Author: liyujie
 * @Date: 2021/07/19
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/07/19
-->
<template>
    <section class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    数据统计
                </div>
            </div>
            <!-- /header -->
            <div class="message-view">
                <div class="msg-item">
                    奖励总支出：<span style="color: #2FBDB3">￥{{ statistics_data.total_award_money }}</span>
                </div>
                <div class="msg-item">
                    参与用户总数：<span style="color: #2FBDB3">{{ statistics_data.total_user_num }}</span>
                </div>
                <div class="msg-item">
                    人均奖励金额：<span style="color: #2FBDB3">￥{{ statistics_data.avg_user_money }}</span>
                </div>
            </div>
            <div class="date-view">
                <div class="date-left">
                    <div class="text">查看日期</div>
                    <el-select
                        v-model="filterData.type"
                        size="medium"
                        style="width: 152px"
                        @change="handleDateChange"
                    >
                        <el-option
                            label="全部"
                            :value="1"
                        />
                        <el-option
                            label="近7天"
                            :value="7"
                        />
                        <el-option
                            label="近30天"
                            :value="30"
                        />
                        <el-option
                            label="自定义"
                            :value="99"
                        />
                    </el-select>
                    <el-date-picker
                        v-model="dateTimeRange"
                        v-if="filterData.type === 99"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        :clearable="false"
                        style="width: 360px;margin-left: 12px"
                        value-format="timestamp"
                    />
                    <div
                        class="time"
                        v-if="filterData.type === 1"
                    >
                        统计截止时间：{{ filterData.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </div>
                    <div
                        class="time"
                        v-else
                    >
                        统计时间：{{ filterData.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }} - {{ filterData.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </div>
                </div>
                <div class="date-right">
                    <el-button
                        type="primary"
                        size="medium"
                        @click="showSettingDialog"
                    >
                        预警设置
                    </el-button>
                </div>
            </div>
            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="table"
                    class="data-table"
                    :cell-style="{height: '40px'}"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                        fixed="left"
                    />
                    <el-table-column
                        label="日期"
                        align="center"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.day }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="budget_money"
                        label="当日预警线(元)"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="total_award_money"
                        label="奖励支出(元)"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="total_user_num"
                        label="参与用户总数"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="avg_money"
                        label="人均奖励金额(元)"
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="操作"
                        align="left"
                        fixed="right"
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="toCreateClassList(scope.row)"
                                >
                                    查看详情
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->
            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <peak-setting-dialog
            ref="peakSettingDialog"
        />
    </section>
</template>

<script>
import { back } from '@/mixin/back';
import PeakSettingDialog from "@/components/create-class/peak-setting-dialog";

export default {
    name: 'CreateClassStatistics',
    mixins: [back],
    components: {PeakSettingDialog},
    data() {
        return {
            filterData: {
                type: 1,
                start_time: '',
                end_time: ''
            },
            dataTimeRange: [],
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            statistics_data: {
                // 邀请总支出
                total_award_money: 0,
                // 总人数
                total_user_num: 0,
                // 人均奖励
                avg_user_money: 0
            },
            authResult: {}
        };
    },
    computed: {},
    filters: {},
    watch: {
        dateTimeRange(value) {
            this.filterData.start_time = value ? value[0] / 1000 : '';
            this.filterData.end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '社交'}, {title: '班级管理'}, {
            title: '创建同校班级',
            path: '/student/create-class'
        }, {title: '数据统计'}]);
        this.filterData.end_time = Number(this.$moment().endOf('day').format("X"));
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/task_stuagent_extra_class_create%5Cget_day_statistic_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                    if(res.data.statistic) {
                        this.statistics_data = res.data.statistic;
                    }
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 重置筛选条件
         */
        handleReset() {
            this.filterData = {};
            this.dateTimeRange = null;
            this.getTableData(1);
        },

        /**
         * 更改日期
         * @param val - 统计天数
         */
        handleDateChange(val) {
            if(val !== 99) {
                this.filterData.end_time = Number(this.$moment().endOf('day').format("X"));
                this.filterData.start_time = Number(this.$moment().subtract(val, 'days').startOf('day').format("X"));
            }
            if(val === 1) {
                this.filterData.start_time = 0;
                this.filterData.end_time = Number(this.$moment().endOf('day').format("X"));
            }
            if(val === 99) {
                this.filterData.end_time = Number(this.$moment().endOf('day').format("X"));
                this.filterData.start_time = Number(this.$moment().startOf('day').format("X"));
            }
            this.dateTimeRange = [this.filterData.start_time * 1000, this.filterData.end_time * 1000];
        },

        /**
         * 自定义表格标题hover提示
         */
        renderHeader(h,title, content){
            return h('span', {}, [
                h('span', {style: 'color:#333; font-weight: 500'}, title),
                h('el-popover', { props: { placement: 'top-start', width: '200', trigger: 'hover', content: content }}, [
                    h('i', { slot: 'reference', class:'el-icon-question', style: 'color:#999'}, '')
                ])
            ]);
        },

        /**
         * 显示峰值设置弹窗
         */
        showSettingDialog() {
            if(!this.authResult.auth_set) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$refs.peakSettingDialog.show();
        },

        /**
         * 跳转邀请列表
         * @param row
         */
        toCreateClassList(row) {
            let day = String(row.day);
            let start_time = Number(this.$moment(day).startOf('day').format("x"));
            let end_time = Number(this.$moment(day).endOf('day').format("x"));

            this.handleRoute('createClass', {
                award_status: 2,
                start_time,
                end_time
            });
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 0 32px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }
        .date-view {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 32px;
            .date-left {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-size: 14px;
                font-weight: 400;
                margin: 16px 0;
                .text {
                    color: #333333;
                    margin-right: 12px;
                }
                .time {
                    color: #999999;
                    margin-left: 12px;
                }
            }
        }
        .message-view {
            display: flex;
            height: 44px;
            background: #EEF8FF;
            align-items: center;
            margin: 16px 32px 0;
            padding: 0 20px;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            .msg-item {
                margin-right: 32px;
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 0 24px;
            margin-top: 12px;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
                .query-button  {
                    width: 84px;
                    height: 36px;
                    background: #FFFFFF;
                    border-radius: 4px;
                    border: 1px solid #1890FF;
                    font-size: 14px;
                    font-family: PingFangSC-Medium, PingFang SC;
                    font-weight: 500;
                    color: #1890FF;
                    margin-right: 12px;
                }
            }
        }

        // 表格
        .table-wrapper {
            padding: 0 32px;
            flex: 1;
            height: 0;
            .cell-title {
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 12px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 2;
            }
            .cell-content {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 3;
            }
            .cell-tips {
                color: #999;
            }
        }
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }
        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #A6A6A6;
                }
            }
        }

        /* 分页 */
        .pagination-wrapper {
            padding: 12px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 0;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
    /deep/ .el-table {
        th {
            background: #f4f4f4!important;
            font-size: 14px!important;
            font-family: PingFangSC-Medium, PingFang SC;
            color: #333333!important;
            .cell {
                font-weight: 500!important;
            }
        }
    }
}
</style>
